package com.cv.platform.dal.es.repository.video;

import com.cv.platform.dal.es.index.video.CvVideoEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * 视频搜索引擎数据层
 *
 * @author Charles_XDXD
 */
@Repository
public interface CvVideoEntityRepository extends ElasticsearchRepository<CvVideoEntity, Long> {

    /**
     * 权重查询，顺序为：标题、昵称、标签、类型
     */
    @Query("{\"bool\": {\"should\": [" +
            "{\"match\": {\"title\": {\"query\": \"?0\", \"boost\": 4}}}," + // 模糊匹配视频标题
            "{\"match\": {\"nickname\": {\"query\": \"?0\", \"boost\": 3}}}," + // 模糊匹配用户昵称
            "{\"constant_score\": {\"filter\": {\"terms\": {\"labels\": [\"?0\"]}}, \"boost\": 2}}," + // 精准匹配标签
            "{\"constant_score\": {\"filter\": {\"term\": {\"type\": \"?0\"}}, \"boost\": 1}}" + // 精准匹配类型
            "]}}")
    Page<CvVideoEntity> searchVideo(String keyword, Pageable pageable);

}
